package com.ne.alice;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 一个线程处理一个连接
 */
public class ConnectionPerThread implements Runnable{
    @Override
    public void run() {
        try{
            ServerSocket serverSocket = new ServerSocket(10089);
            while (!Thread.interrupted()){
                Socket socket = serverSocket.accept();
                Handler handler = new Handler(socket);
                new Thread(handler).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class Handler implements Runnable{
        final Socket socket;

        Handler(Socket s){
            socket = s;
        }

        @Override
        public void run() {
            while (true){
                try{
                    byte[] input = new byte[1024];
                    socket.getInputStream().read(input);
                    byte[] output = null;
                    socket.getOutputStream().write(output);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
